#include "modifyusrinfo.h"
#include "ui_modifyusrinfo.h"
#include"tipwidget.h"
#include"messageboxwidget.h"
#include<QDebug>
#include<QMessageBox>
#include <QRegularExpression>
ModifyUsrInfo::ModifyUsrInfo(const QString&usrName,const QString&password,QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::ModifyUsrInfo)
{
    ui->setupUi(this);
    setFixedSize(570,440);
    setAttribute(Qt::WA_DeleteOnClose);
    setWindowModality(Qt::ApplicationModal);
    ui->submitBtn->setShortcut(Qt::Key_Return);
    m_usrName=usrName;
    m_password=password;
    ui->submitBtn->setEnabled(false);
    ui->usrNameLineEdit->setText(usrName);
    ui->usrNameLineEdit->setEnabled(false);
    ui->passwordWidget->setVisible(false);
    ui->newPasswordWidget->setVisible(false);
}

ModifyUsrInfo::~ModifyUsrInfo()
{
    delete ui;
}

bool ModifyUsrInfo::validateString(const QString &input)
{
    QRegularExpression regex("^[A-Za-z0-9_]{1,31}$");
    QRegularExpressionMatch match = regex.match(input);
    if (!match.hasMatch()) {
        return false;
    }
    return true;
}

void ModifyUsrInfo::on_cancelBtn_clicked()
{
    close();
}



void ModifyUsrInfo::on_comboBox_currentIndexChanged(int index)
{
    ui->usrNameLineEdit->setText(m_usrName);
    switch (index) {
    case 0:{
        ui->submitBtn->setEnabled(false);
        ui->usrNameLineEdit->setEnabled(false);
        ui->passwordWidget->setVisible(false);
        ui->newPasswordWidget->setVisible(false);
        break;
    }
    case 1:{
        ui->submitBtn->setEnabled(true);
        ui->usrNameLineEdit->setEnabled(true);
        ui->passwordLabel->setText("密  码：");
        ui->passwordWidget->setVisible(true);
        ui->passwordLineEdit->clear();
        ui->newPasswordWidget->setVisible(false);
        break;
    }
    case 2:{
        ui->submitBtn->setEnabled(true);
        ui->usrNameLineEdit->setEnabled(false);
        ui->passwordLabel->setText("旧密码：");
        ui->passwordWidget->setVisible(true);
        ui->passwordLineEdit->clear();
        ui->newPasswordWidget->setVisible(true);
        ui->newPwdLineEdit->clear();
        break;
    }
    case 3:{
        ui->submitBtn->setEnabled(true);
        ui->usrNameLineEdit->setEnabled(true);
        ui->passwordLabel->setText("旧密码：");
        ui->passwordWidget->setVisible(true);
        ui->passwordLineEdit->clear();
        ui->newPasswordWidget->setVisible(true);
        ui->newPwdLineEdit->clear();
        break;
    }
    default:
        break;
    }
}


void ModifyUsrInfo::on_submitBtn_clicked()
{
    //emit一个信号
    switch (ui->comboBox->currentIndex()) {
    case 0:{

        break;
    }
    case 1:{
        //修改用户名
        QString name=ui->usrNameLineEdit->text();
        QString pwd=ui->passwordLineEdit->text();
        if(!validateString(name)){
            QMessageBox::critical(this,"critical","用户名只能由英文大小写字母以及数字和下划线组成，且最大长度为31！");
            return;
        }
        if(!validateString(pwd)){
            QMessageBox::critical(this,"critical","密码只能由英文大小写字母以及数字和下划线组成，且最大长度为31！");
            return;
        }
        if(pwd!=m_password){
            TipWidget::getInstance()->showTip("密码错误,请检查",
                                              TipWidget::Info,
                                              1500,
                                              this);
            return;
        }

        MessageBoxWidget *widget=new MessageBoxWidget;
        widget->setMessageBox("当前修改操作不可逆，是否继续？",
                              MessageBoxWidget::Warning,
                              this);
        connect(widget,&MessageBoxWidget::confirmSignal,this,[=](){
            emit sendModifyNewItems(name,m_password);
            close();
        });
        connect(widget,&MessageBoxWidget::cancelSignal,this,[=](){
            close();
        });
        widget->show();


        break;
    }
    case 2:{
        //修改密码
        QString oldPwd=ui->passwordLineEdit->text();
        QString newPwd=ui->newPwdLineEdit->text();
        if((!validateString(newPwd))||(!validateString(oldPwd))){
            QMessageBox::critical(this,"critical","密码只能由英文大小写字母以及数字和下划线组成，且最大长度为31！");
            return;
        }
        if(oldPwd!=m_password){
            TipWidget::getInstance()->showTip("旧密码错误,请检查",
                                              TipWidget::Info,
                                              1500,
                                              this);
            return;
        }

        MessageBoxWidget *widget=new MessageBoxWidget;
        widget->setMessageBox("当前修改操作不可逆，是否继续？",
                              MessageBoxWidget::Warning,
                              this);
        connect(widget,&MessageBoxWidget::confirmSignal,this,[=](){
            emit sendModifyNewItems(m_usrName,newPwd);
            close();
        });
        connect(widget,&MessageBoxWidget::cancelSignal,this,[=](){
            close();
        });
        widget->show();
        break;
    }
    case 3:{
        //修改用户名和密码
        QString name=ui->usrNameLineEdit->text();
        QString oldPwd=ui->passwordLineEdit->text();
        QString newPwd=ui->newPwdLineEdit->text();
        if(!validateString(name)){
            QMessageBox::critical(this,"critical","用户名只能由英文大小写字母以及数字和下划线组成，且最大长度为31！");
            return;
        }
        if((!validateString(newPwd))||(!validateString(oldPwd))){
            QMessageBox::critical(this,"critical","密码只能由英文大小写字母以及数字和下划线组成，且最大长度为31！");
            return;
        }
        if(oldPwd!=m_password){
            TipWidget::getInstance()->showTip("旧密码错误,请检查",
                                              TipWidget::Info,
                                              1500,
                                              this);
            return;
        }

        MessageBoxWidget *widget=new MessageBoxWidget;
        widget->setMessageBox("当前修改操作不可逆，是否继续？",
                              MessageBoxWidget::Warning,
                              this);
        connect(widget,&MessageBoxWidget::confirmSignal,this,[=](){
            emit sendModifyNewItems(name,newPwd);
            close();
        });
        connect(widget,&MessageBoxWidget::cancelSignal,this,[=](){
            close();
        });
        widget->show();

        break;
    }
    default:
        break;
    }
}
